package com.lins.framework.utils.address;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.lins.framework.R;
import com.lins.framework.utils.Validator;
import com.lins.framework.utils.tip.ToastUtil;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Admin on 2017/5/9.
 */

public class AddressSelectDialogFragment extends DialogFragment {

    @BindView(R.id.id_tv_title)
    TextView idTvTitle;
    @BindView(R.id.id_ryc)
    RecyclerView idRyc;
    @BindView(R.id.id_btn_negative)
    Button idBtnNegative;
    @BindView(R.id.id_btn_positive)
    Button idBtnPositive;

    private AreaParsingPresenter presenter;
    private AddressSelectListAdapter adapter;
    private ArrayList<AreaParsingBean> mProvinceList;

    private String province = "", city = "", county = "";
    private ArrayList<String> pathList;

    public static AddressSelectDialogFragment newInstance(int level) {

        Bundle args = new Bundle();

        AddressSelectDialogFragment fragment = new AddressSelectDialogFragment();
        args.putInt("level", level);
        fragment.setArguments(args);
        return fragment;
    }

    public int getLevel() {
        return getArguments().getInt("level");
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View rootView = inflater.inflate(R.layout.app_cm_city_select_dialog, null);
        ButterKnife.bind(this, rootView);
        initView();
        initData();
        initListener();
        builder.setView(rootView);
        return builder.create();
    }

    private void initView() {
        idTvTitle.setText("选择城市");
        idBtnNegative.setText("返回");
        idBtnPositive.setText("确定");
        idRyc.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new AddressSelectListAdapter(getActivity(), new ArrayList<AreaParsingBean>());
        idRyc.setAdapter(adapter);
    }

    private void initData() {
        presenter = new AreaParsingPresenter(getActivity());
        mProvinceList = presenter.getProvinceList();
        adapter.addList(mProvinceList);
    }

    private void initListener() {
        adapter.setOnItemClickListener(new AddressSelectListAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(String pathTemp, String title, int position) {
                idBtnNegative.setText("上一级");
                String[] arr = pathTemp.split(",");
                pathList = new ArrayList<>();
                for (String s : arr) {
                    pathList.add(s);
                }
                if (pathList.size() == 2) {
                    province = title;
                    city = "";
                    county = "";
                    if (getLevel() >= 2)
                        adapter.refresh(presenter.getCityList(title));
                } else if (pathList.size() == 3) {
                    city = title;
                    county = "";
                    if (getLevel() == 3)
                        adapter.refresh(presenter.getCountyList(title, pathTemp));
                } else if (pathList.size() == 4) {
                    county = title;
                }
                idTvTitle.setText(province + city + county);
            }
        });

    }

    @OnClick({R.id.id_btn_negative, R.id.id_btn_positive})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.id_btn_negative:
                if (pathList != null && pathList.size() != 0) {
                    switch (pathList.size()) {
                        case 4:
                            pathList.remove(3);
                        case 3:
                            pathList.remove(2);
                            city = "";
                            county = "";
                            adapter.refresh(presenter.getCityList(province));
                            break;
                        case 2:
                            province = "";
                            pathList.clear();
                            adapter.refresh(mProvinceList);
                            idBtnNegative.setText("返回");
                            break;
                    }
                    String address = province + city + county;
                    if (Validator.isEmpty(address))
                        address = "选择城市";
                    idTvTitle.setText(address);
                } else
                    dismiss();
                break;
            case R.id.id_btn_positive:
                if (pathList == null || (pathList.size() != 4 && getLevel() == 3)
                        || (pathList.size() != 3 && getLevel() == 2) || (pathList.size() != 2 && getLevel() == 1))
                    ToastUtil.showToast(getActivity(), "请选择地址");
                else {
                    OnAddressSelectListener listener = (OnAddressSelectListener) getActivity();
                    listener.OnSelectComplete(idTvTitle.getText().toString(), pathList);
                    dismiss();
                }
                break;
        }
    }

    public interface OnAddressSelectListener {
        void OnSelectComplete(String address, ArrayList<String> pathList);
    }
}
